/**
 * @ClassName: UserInvoiceUtil
 * @Description:鍙戠エ琛�
 * @author 832
 * @date 2016-12-08
 */
package com.fzrskj.cloud.free.user.utils;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.dic.bean.DicParamItem;
import com.fzrskj.cloud.free.dic.utils.DicParamItemUtils;
import com.fzrskj.cloud.free.user.dao.UserInvoiceDao;
import com.fzrskj.cloud.free.user.bean.UserInvoice;
import com.fzrskj.cloud.free.user.bean.UserInvoiceView;
import ms.core.object.CObject;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserInvoiceUtil extends CObject{
	
	/**
	 * 新增发票数据
	 * @param userInvoice
	 * @return
	 */
	public int addUserInvoice(UserInvoice userInvoice, String paramEName, String value){
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();			
		DicParamItem paramItem = dicParamItemUtils.getParamItemByENameAndValue(paramEName, value, 0);		
		if (paramItem == null) {
			UserLogUtils.addLog(this, userInvoice.getUserId(), "查询系统参数失败");
			this.setErrCode("查询系统参数失败");
			return -1;
		}
		Double money = CStrTool.strToDoubleDef(paramItem.getRemark(),-1);
		if(money<0){
			UserLogUtils.addLog(this, userInvoice.getUserId(), "选择发票获取途径有误！");
			this.setErrCode("选择发票获取途径有误！");
			return -1;
		}
		
		//查询模板名称是否重复 
		if(!CStrTool.isBlankStr(userInvoice.getName())){
			List<UserInvoice> listInvoice= queryName(userInvoice.getName(), userInvoice.getUserName());
			if(listInvoice!=null && listInvoice.size()>0){
				this.setErrCode(AppCode.INVOICENAME_EXISTS);
				return 0;
			}
		}
				
		UserInvoiceDao dao = new UserInvoiceDao();
		Map<String,Object> values = new HashMap<String, Object>();
		values.put("type", userInvoice.getType());
		values.put("header", userInvoice.getHeader());
		values.put("identityId", userInvoice.getIdentityId());
		values.put("bankName", userInvoice.getBankName());
		values.put("openAccount", userInvoice.getOpenAccount());
		values.put("registerAddress", userInvoice.getRegisterAddress());
		values.put("contactNumber", userInvoice.getContactNumber());
		values.put("materialUrl", userInvoice.getMaterialUrl());
		values.put("price", userInvoice.getPrice());
		values.put("content", userInvoice.getContent());
		values.put("recipientAddress", userInvoice.getRecipientAddress());
		values.put("companyName", userInvoice.getCompanyName());
		values.put("postcode", userInvoice.getPostcode());
		values.put("addressee", userInvoice.getAddressee());
		values.put("telephone", userInvoice.getTelephone());
		values.put("remark", userInvoice.getRemark());
		values.put("getType", userInvoice.getGetType());
		values.put("userId", userInvoice.getUserId());
		values.put("userName", userInvoice.getUserName());
		values.put("createTime", userInvoice.getCreateTime());
		values.put("status", "N");
		values.put("name", userInvoice.getName());
		//扣款
		FuncHandleUtils funUtil = new FuncHandleUtils();
		if(money>0){
			String ret = funUtil.FinanceAccount(userInvoice.getUserName(), 300, "会员【" + userInvoice.getUserName()+ "】申请发票业务扣款:【" + money + "】", "", "", "", money);
			JSONObject json = JSONObject.fromObject(ret);		
			if (("-1").equals(json.getString("code"))) {
				this.setErrCode(json.getString("message"));
				dao.dispose();
				return -1;
			}
		}
		//新增发票
		int ret = dao.addUserInvoice(values);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}
	
	/**
	 * 查询符合条件的发票总数
	 * @param cdt
	 * @return
	 */
	public int getUserInvoiceCount(Map<String,Object> cdt){
		UserInvoiceDao dao = new UserInvoiceDao();
		int ret = dao.getUserInvoiceCount(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}
	
	/**
	 * 查询全部发票
	 * @param cdt
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public  List<UserInvoiceView> getAllInvoice(Map<String,Object> cdt, int page, int pageSize){
		UserInvoiceDao dao = new UserInvoiceDao();
		List<UserInvoiceView> getUserInvoice = dao.getAllInvoice(cdt,page,pageSize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return getUserInvoice;
	}
	
	/**
	 * 删除发票记录
	 * @param userName
	 * @param id
	 * @return
	 */
	public int delinvoice(String userName,int id){
		UserInvoiceDao dao = new UserInvoiceDao();
		int ret = dao.delinvoice(userName, id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}
	
	/**
	 * 更新发票信息
	 * @param userInvoice
	 * @return
	 */
	public int uptInvoice(UserInvoice userInvoice){
		UserInvoiceDao dao = new UserInvoiceDao();
		Map<String,Object> value = new HashMap<String, Object>();
		value.put("type", userInvoice.getType());
		value.put("header", userInvoice.getHeader());
		value.put("identityId", userInvoice.getIdentityId());
		value.put("bankName", userInvoice.getBankName());
		value.put("openAccount", userInvoice.getOpenAccount());
		value.put("registerAddress", userInvoice.getRegisterAddress());
		value.put("contactNumber", userInvoice.getContactNumber());
		value.put("materialUrl", userInvoice.getMaterialUrl());
		value.put("price", userInvoice.getPrice());
		value.put("content", userInvoice.getContent());
		value.put("recipientAddress", userInvoice.getRecipientAddress());
		value.put("companyName", userInvoice.getCompanyName());
		value.put("postcode", userInvoice.getPostcode());
		value.put("addressee", userInvoice.getAddressee());
		value.put("telephone", userInvoice.getTelephone());
		value.put("remark", userInvoice.getRemark());
		value.put("getType", userInvoice.getGetType());
		value.put("status", userInvoice.getStatus());
		
		Map<String,Object> cdt = new HashMap<String, Object>();
		cdt.put("id", userInvoice.getId());
		cdt.put("userId", userInvoice.getUserId());
		
		int uptInvoice = dao.uptInvoice(value,cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return uptInvoice;
	}
	
	/**
	 * 发票审核
	 * @return
	 */
	public int invoiceAudit(Map<String,Object> values,Map<String,Object> cdt){
		UserInvoiceDao dao = new UserInvoiceDao();
		int uptInvoice = dao.uptInvoice(values,cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return uptInvoice;
	}
	
	/**
	 * 获取发票信息
	 * @param id
	 * @param userId
	 * @return
	 */
	public UserInvoiceView queryUserInvoice(int id,int userId){
		UserInvoiceDao dao = new UserInvoiceDao();
		UserInvoiceView ret = dao.queryUserInvoice(id, userId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	} 
	
	/**
	 * 根据用户名查询历史
	 * @param userName
	 * @return
	 */
	public List<UserInvoice> queryHistory(String userName){
		UserInvoiceDao dao = new UserInvoiceDao();
		List<UserInvoice> queryHistory = dao.queryHistory(userName);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryHistory;
	}
	
	/**
	 * 根据ID查找数据
	 * @return
	 */
	public UserInvoice queryUIById(int id){
		UserInvoiceDao dao = new UserInvoiceDao();
		UserInvoice queryId = dao.queryUIById(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryId;
	}
	/**
	 * 查询发票模板是否存在
	 * @param name
	 * @param userName
	 * @return
	 */
	public List<UserInvoice> queryName(String name,String userName){
		UserInvoiceDao dao = new UserInvoiceDao();
		List<UserInvoice> queryName = dao.queryName(name, userName);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return queryName;
	} 
}